import org.junit.jupiter.api.Test;

import java.lang.reflect.InvocationTargetException;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ShopTest {

    // 测试苹果和草莓的总价格
    @Test
    void testAppleAndStrawberryTotalPrice() {
        // 创建购物车对象
        Cart cart = new Cart();
        // 向购物车中添加苹果和草莓
        cart.addFruit("Apple", 2);
        cart.addFruit("Strawberry", 3);

        // 计算购物车中水果的总价格
        double totalPrice = cart.calculateTotalPrice();
        // 断言总价格是否为55.0
        assertEquals(55.0, totalPrice, 0.001);
    }

    // 测试添加芒果
    @Test
    void testAddMango() {
        // 创建购物车对象
        Cart cart = new Cart();
        // 向购物车中添加苹果和草莓
        cart.addFruit("Apple", 2);
        cart.addFruit("Strawberry", 3);
        // 向购物车中添加芒果
        cart.addFruit("Mango", 4);

        // 计算购物车中水果的总价格
        double totalPrice = cart.calculateTotalPrice();
        // 断言总价格是否为135.0
        assertEquals(135.0, totalPrice, 0.001);
    }

    // 测试草莓打折
    @Test
    void testStrawberryWithDiscount() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        // 创建购物车对象
        Cart cart = new Cart();
        // 通过反射获取打折策略类
        Class<?> discountClass = Class.forName("EightDiscount");
        // 创建打折策略对象
        DiscountStrategy discountStrategy = (DiscountStrategy) discountClass.getDeclaredConstructor().newInstance();

        // 向购物车中添加苹果和草莓
        cart.addFruit("Apple", 2);
        // 向购物车中添加打折的草莓
        cart.addFruit("strawberry_discount", 3,discountStrategy);
        // 向购物车中添加芒果
        cart.addFruit("Mango", 4);

        // 计算购物车中水果的总价格
        double totalPrice = cart.calculateTotalPrice();
        // 断言总价格是否为127.2
        assertEquals(127.2, totalPrice, 0.001);
    }

    // 测试购物车打折
    @Test
    void testCartWithDiscount() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        // 创建购物车对象
        Cart cart = new Cart();
        // 创建购物车代理对象
        CartProxy cart_proxy = new CartProxy(cart);
        // 通过反射获取打折策略类
        Class<?> discountClass = Class.forName("EightDiscount");
        // 创建打折策略对象
        DiscountStrategy discountStrategy = (DiscountStrategy) discountClass.getDeclaredConstructor().newInstance();

        // 向购物车中添加苹果和草莓
        cart.addFruit("Apple", 2);
        // 向购物车中添加打折的草莓
        cart.addFruit("strawberry_discount", 3,discountStrategy);
        // 向购物车中添加芒果
        cart.addFruit("Mango", 4);
        // 计算购物车中水果的总价格
        double finalPrice = cart_proxy.calculateTotalPrice();
        // 断言总价格是否为117.2
        assertEquals(117.2, finalPrice, 0.001);
    }

    // 测试购物车不打折
    @Test
    void testCartWithDiscount_NO() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        // 创建购物车对象
        Cart cart = new Cart();
        // 创建购物车代理对象
        CartProxy cart_proxy = new CartProxy(cart);
        // 通过反射获取打折策略类
        Class<?> discountClass = Class.forName("EightDiscount");
        // 创建打折策略对象
        DiscountStrategy discountStrategy = (DiscountStrategy) discountClass.getDeclaredConstructor().newInstance();

        // 向购物车中添加苹果和草莓
        cart.addFruit("Apple", 1);
        // 向购物车中添加打折的草莓
        cart.addFruit("strawberry_discount", 3,discountStrategy);
        // 向购物车中添加芒果
        cart.addFruit("Mango", 3);
        // 计算购物车中水果的总价格
        double finalPrice = cart_proxy.calculateTotalPrice();
        // 断言总价格是否为99.2
        assertEquals(99.2, finalPrice, 0.001);
    }

}
